package org.wzq;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.wzq.bean.School;
import org.wzq.bean.Student;

public class Main {
    public static void main(String[] args) {
        // connection to db
        Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();

        // the school
        School school = new School();
        school.setName("小学");
        school.setAddr("gz");

        // lilei  & hmm
        Student stu_LiLei = new Student();
        stu_LiLei.setName("lilei");
        stu_LiLei.setSchool(school);

        // 写入数据库,如果不用tx则出错
        Transaction tx = session.beginTransaction();
        // session.save(school); // stu.hbm.xml  casecade='save-update'
        session.save(stu_LiLei);
        tx.commit();

        // 读 TODO
        // 中文 TODO

        session.close();
    }
}
